﻿Imports Microsoft.SqlServer.Dts.Runtime

<DtsTask(DisplayName:="Task 'Send SMS'", _
    Description:="Send SMS over the Internet", _
    IconResource:="SSISComponents.SMSTask.SMS.ico", _
    UITypeName:="SSISComponents.SMSTask.SMSTaskUI, SSISComponents.DTS.Tasks.SMSTaskUI, Version=1.2.0.0, Culture=Neutral, PublicKeyToken=e29d7b7f099282ca", _
    TaskContact:="Tillmann Eitelberg, SSIS-Components.net", _
    RequiredProductLevel:=DTSProductLevel.None, _
    TaskType:="DTS100")> _
Public Class SMSTask
    Inherits Task

#Region "Variables"

    Private Services As Services
    Private _Username As String = Nothing
    Private _Password As String = Nothing
    Private _Type As String = Nothing
    Private _Text As String = Nothing
    Private _Sender As String = Nothing
    Private _Recipient As String = Nothing
    Private _ServiceName As String = Nothing
    Private _HTTPS As Boolean = False
    Private _Delay As String = Nothing
    Private _Parameter As String = Nothing

#End Region

#Region "Properties"

    Public Property Username() As String
        Get
            Return _Username
        End Get
        Set(ByVal value As String)
            _Username = value
        End Set
    End Property

    Public Property Type() As String
        Get
            Return _Type
        End Get
        Set(ByVal value As String)
            _Type = value
        End Set
    End Property

    Public Property Text() As String
        Get
            Return _Text
        End Get
        Set(ByVal value As String)
            _Text = value
        End Set
    End Property

    Public Property Sender() As String
        Get
            Return _Sender
        End Get
        Set(ByVal value As String)
            _Sender = value
        End Set
    End Property

    Public Property Recipient() As String
        Get
            Return _Recipient
        End Get
        Set(ByVal value As String)
            _Recipient = value
        End Set
    End Property

    Public Property Password() As String
        Get
            Return _Password
        End Get
        Set(ByVal value As String)
            _Password = value
        End Set
    End Property

    Public Property ServiceName() As String
        Get
            Return _ServiceName
        End Get
        Set(ByVal value As String)
            _ServiceName = value
        End Set
    End Property

    Public Property HTTPS() As Boolean
        Get
            Return _HTTPS
        End Get
        Set(ByVal value As Boolean)
            _HTTPS = value
        End Set
    End Property

    Public Property Delay() As String
        Get
            Return _Delay
        End Get
        Set(ByVal value As String)
            _Delay = value
        End Set
    End Property

    Public Property Parameter() As String
        Get
            Return _Parameter
        End Get
        Set(ByVal value As String)
            _Parameter = value
        End Set
    End Property

#End Region

    Public Overrides Sub InitializeTask(ByVal connections As Microsoft.SqlServer.Dts.Runtime.Connections, ByVal variableDispenser As Microsoft.SqlServer.Dts.Runtime.VariableDispenser, ByVal events As Microsoft.SqlServer.Dts.Runtime.IDTSInfoEvents, ByVal log As Microsoft.SqlServer.Dts.Runtime.IDTSLogging, ByVal eventInfos As Microsoft.SqlServer.Dts.Runtime.EventInfos, ByVal logEntryInfos As Microsoft.SqlServer.Dts.Runtime.LogEntryInfos, ByVal refTracker As Microsoft.SqlServer.Dts.Runtime.ObjectReferenceTracker)
        MyBase.InitializeTask(connections, variableDispenser, events, log, eventInfos, logEntryInfos, refTracker)
    End Sub

    Public Overrides Function Validate(ByVal connections As Microsoft.SqlServer.Dts.Runtime.Connections, ByVal variableDispenser As Microsoft.SqlServer.Dts.Runtime.VariableDispenser, ByVal componentEvents As Microsoft.SqlServer.Dts.Runtime.IDTSComponentEvents, ByVal log As Microsoft.SqlServer.Dts.Runtime.IDTSLogging) As Microsoft.SqlServer.Dts.Runtime.DTSExecResult

        Dim isBaseValid As Boolean = True

        If Not MyBase.Validate(connections, variableDispenser, componentEvents, log) = DTSExecResult.Success Then
            componentEvents.FireError(0, "SMSTask", "Base validation failed", "", 0)
            Return DTSExecResult.Failure
        Else
            Return DTSExecResult.Success
        End If

    End Function

    Public Overrides Function Execute(ByVal connections As Microsoft.SqlServer.Dts.Runtime.Connections, ByVal variableDispenser As Microsoft.SqlServer.Dts.Runtime.VariableDispenser, ByVal componentEvents As Microsoft.SqlServer.Dts.Runtime.IDTSComponentEvents, ByVal log As Microsoft.SqlServer.Dts.Runtime.IDTSLogging, ByVal transaction As Object) As Microsoft.SqlServer.Dts.Runtime.DTSExecResult
        Try
            Dim send As Boolean = False
            Services = New Services
            For Each _service As IService In Services.IServices
                If _service.Name = Me.ServiceName Then
                    _service.Delay = Me.Delay
                    _service.HTTPS = Me.HTTPS
                    _service.Password = Me.Password
                    _service.Recipient = Me.Recipient
                    _service.Sender = Me.Sender
                    _service.Text = Me.Text
                    _service.Type = Me.Type
                    _service.Username = Me.Username
                    _service.Parameter = Me.Parameter
                    _service.Send()
                    send = True
                    Return MyBase.Execute(connections, variableDispenser, componentEvents, log, transaction)
                    componentEvents.FireInformation(0, "SMSTask", "The SMS to " + Me.Recipient.ToString() + " has been successfully sent", "", 0, True)
                    Exit For
                End If
            Next
            If send = False Then
                componentEvents.FireError(0, "SMSTask", "You must specify a SMS Service.", "", 0)
                Return DTSExecResult.Failure
            End If
        Catch ex As Exception
            componentEvents.FireError(0, "SMSTask", ex.Message, "", 0)
            Return DTSExecResult.Failure
        End Try

    End Function


End Class
